<template>
  <div>
    <div class="top-panel">
      <el-card>
        <el-form model="searchForm" label-width="70px" label-position="right">
          <el-row>
            <el-col :span="5">
              <el-form-item label="用户名">
                <el-input
                    class="password-input"
                    v-model="searchForm.userNameFuzzy"
                    clearable
                    placeholder="支持模糊搜索"
                    @keyup.enter.native="loadDataList">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="手机号">
                <el-input class="password-input"
                          v-model="searchForm.phoneFuzzy"
                          clearable
                          placeholder="支持模糊搜索"
                          @keyup.enter.native="loadDataList">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4" :style="{paddingLeft:'10px'}">
              <el-button type="success" @click="loadDataList()">查询</el-button>
              <el-button type="primary" @click="showEdit()" v-has="proxy.PermissionCode.account.edit">新增用户</el-button>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </div>

    <!-- 内容区域 -->
    <el-card :style="{ height: 'calc(100vh - 192px)',marginTop:'10px'}">
      <template #header>
        <span>用户列表</span>
      </template>
      <Table
          ref="tableInfoRef"
          :columns="columns"
          :fetch="loadDataList"
          :dataSource="tableData"
          :options="tableOptions"
          :extHeight="tableOptions.extHeight"
          >
        <template #slotStatus="{ index,row }">
          <span style="color:red" v-if="row.status ==0">禁用</span>
          <span style="color:green" v-else>启用</span>
        </template>
        <template #slotOperation="{ index,row }">
          <div class="row-op-panel"
          v-if="!(userInfo.superAdmin && userInfo.userId === row.userId)">
            <a
                class="a-link"
                href="javascript:void(0)"
                @click="showEdit(row)"
                v-has="proxy.PermissionCode.account.edit"
            >修改</a>
            <!--  v-has="proxy.PermissionCode.role.edit"
             v-has="proxy.PermissionCode.role.del" -->
            <a
                class="a-link"
                href="javascript:void(0)"
                @click.prevent="editPassword(row)"
                v-has="proxy.PermissionCode.account.updatePwd"
            >修改密码</a>
            <a
                class="a-link"
                href="javascript:void(0)"
                @click.prevent="editStatus(row)"
                v-has="proxy.PermissionCode.account.updateStatus"
            >{{ row.status == 0 ? "启用" : "禁用" }}</a>
            <a
                class="a-link"
                href="javascript:void(0)"
                @click.prevent="delRole(row)"
                v-has="proxy.PermissionCode.account.del"
            >删除</a>
          </div>
        </template>
      </Table>
    </el-card>
  </div>
</template>


<script setup>
import {onMounted, ref, getCurrentInstance, nextTick} from "vue";

const {proxy} = getCurrentInstance();
const userInfo = JSON.parse(sessionStorage.getItem("userInfo"))
const api = {
  loadDataList: "/settings/loadAccountList",
  delAccount: "/settings/delAccount",
  updateStatus: "/settings/updateStatus",
}

const searchForm = ref({})

const tableData = ref({});
const tableOptions = ref({
  extHeight: 125,
})

const columns = [
  {
    label: "用户名",
    prop: "userName",
    width: 150
  },
  {
    label: "手机",
    prop: "phone",
    width: 200,
  },
  {
    label: "拥有角色",
    prop: "roleNames"
  },
  {
    label: "创建时间",
    prop: "createTime",
    width: 200
  },
  {
    width: 100,
    label: "状态",
    prop: "status",
    scopedSlots: "slotStatus"
  },
  {
    width: 190,
    label: "操作",
    prop: "operation",
    scopedSlots: "slotOperation"
  },
]

const tableInfoRef = ref();

const loadDataList = async () => {
  let params = {
    pageNo: tableData.value.pageNo,
    pageSize: tableData.value.pageSize,
    ...searchForm.value
  }
  let result = await proxy.Request({
    url: api.loadDataList,
    params: params,
  })
  if (!result) {
    return;
  }
  tableData.value = result.data;
}

// 修改角色
const showEdit = () => {
  alert("未开发")
}
// 删除角色
const delRole = (data) => {
  proxy.Confirm(`你确定要删除用户${data.userName}吗？`, async () => {
    // let result = await proxy.Request({
    //   url: api.delRole,
    //   params: {
    //     roleId: data.roleId
    //   },
    // })
    // if (!result) {
    //   return;
    // }
    proxy.Message.success("删除成功");
    // loadDataList();
  })
}
// 修改状态
const editStatus = (row) => {
  let status = row.status === 1 ? '0' : '1';
  let info = status === '1' ? '禁用' : '启用'
  proxy.Confirm(`你确定要${info} [用户${row.userName}] 吗？`, async () => {
    let result = await proxy.Request({
      url: api.updateStatus,
      params: {
        userId: row.userId,
        status
      },
    })
    if (!result) {
      return;
    }
    proxy.Message.success("修改用户状态成功");
    // 改变数据
    loadDataList();
  })
}
// 修改密码
const editPassword = () => {
  alert("没有权限")
}
</script>


<style scoped lang="scss">
.box-card {
  .top {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .tree-panel {
    overflow: auto;
    height: calc(100vh - 304px);
  }
}
</style>
